<?php

class Producto_model extends CI_Model {
    var $CI;
    var $tables;
    var $total_tables = array();
     
     
    function __construct(){   
        parent::__construct();
        $this->CI = & get_instance();
        $this->CI->config->load('database_tables');
        $this->total_tables = $this->CI->config->item('dbtables');        
    }
    
    function get_municipio($provincia = '', $municipio = ''){
        if($provincia == '' && $municipio != ''){
            $this->db->select('id');
            $this->db->from($this->total_tables['municipios']);
            $this->db->where('id', $municipio);
            $query = $this->db->get();
            if($query->num_rows() > 0){
                $row = $query->row_array();
                $provincia = $row['fk_id_provincia'];
            }
        }
        $this->db->select('id AS "id", nombre AS "desc"');
        $this->db->from($this->total_tables['municipios']);
        $this->db->order_by('nombre');
        if($provincia != '' && $municipio != ''){
            $this->db->where('id', $municipio);
        }
        $this->db->where('fk_id_provincia', $provincia);
        return $this->db->get();        
    }
    
    function getReservasJson($producto = '', $mes = ''){
        $query = $this->db->query("select id, DATE_FORMAT(fecha_desde, '%Y-%m-%d' ) as 'fdesde', DATE_FORMAT(fecha_hasta, '%Y-%m-%d' ) as 'fhasta',
            nombre_cliente, observaciones, reserva_cierre, confirmada
            from ".$this->total_tables['reserva']." where fk_id_empresa = ". $this->ion_auth->user()->row()->company." and fk_id_producto = ".$producto);
        $res = array();
        if($query->num_rows() > 0){
            foreach ($query->result_array() as $key => $value){
                $detalle = array();
                $detalle['id'] = $value['id'];
                $detalle['start'] = $value['fdesde'];
                $detalle['end'] = $value['fhasta'];
                $detalle['title'] = $value['nombre_cliente'];
                $detalle['allDay'] = true;
                $detalle['reserva_cierre'] = $value['reserva_cierre'];
                //$detalle['description'] = $value['observaciones'];
                if($value['reserva_cierre'] == 'C'){
                    $detalle['className'] = 'fc-event-skin-red';
                    $detalle['url'] = "#";
                }else{
                    if($value['confirmada'] == 'S'){
                        $detalle['className'] = 'fc-event-skin-green';
                        $detalle['url'] = site_url('crud/reserva/ver/'.$value['id']);
                    }else{
                        $detalle['className'] = 'fc-event-skin';
                        $detalle['url'] = "#";
                    }
                }
                $res[] = $detalle;
            }
        }
        
        return $res;
    }  
    
    function getTarifasJson($producto = '', $mes = ''){
        $this->db->where('fk_id_empresa', $this->ion_auth->user()->row()->company);
        $this->db->where('fk_id_producto', $producto);
        //$this->db->where('Month( tar_fecha ) = ', ($mes != '' ? $mes : date('m')));
        $this->db->from($this->total_tables['tarifa']);
        $query = $this->db->get();
        $res = array();
        if($query->num_rows() > 0){
            foreach ($query->result_array() as $key => $value){
                $detalle = array();
                $ano = substr($value['fecha'], 0, 4);
                $mes = substr($value['fecha'], 5, 2);
                $dia = substr($value['fecha'], 8, 2);
                $detalle['ano'] = $ano;
                $detalle['mes'] = $mes-1;
                $detalle['dia'] = $dia;
                $detalle['importe'] = $value['importe']."€";
                $res[] = $detalle;                
            }
        }
        return $res;
    }
    
    function agregar_cierres($producto = '', $fechadesde = '', $fechahasta = ''){
        $query = $this->db->query("SELECT * FROM (".$this->total_tables['reserva'].") 
            WHERE `fk_id_empresa` = ".$this->ion_auth->user()->row()->company."
            AND `fk_id_producto` = ".$producto."
            AND ".$fechadesde." BETWEEN date_format(fecha_desde, '%Y%m%d') AND date_format(fecha_hasta, '%Y%m%d') 
            OR ".$fechahasta." BETWEEN date_format(fecha_desde, '%Y%m%d') AND date_format(fecha_hasta, '%Y%m%d')");
        if($query->num_rows() > 0){
            return false;
        }else{
            $data = array(
                'fecha_desde' => $fechadesde,
                'fecha_hasta' => $fechahasta,
                'nombre_cliente' => lang('cerrado'),
                'email_cliente' => lang('cerrado'),
                'fk_id_producto' => $producto,
                'reserva_cierre' => 'C',
                'fk_id_empresa' => $this->ion_auth->user()->row()->company
            );
            $this->db->insert($this->total_tables['reserva'], $data);
            return true;
        }
    }
    
    function agregar_tarifas($producto = '', $dia = '', $importe = ''){
        $this->db->where('fk_id_empresa', $this->ion_auth->user()->row()->company);
        $this->db->where('fk_id_producto', $producto);
        $this->db->where('fecha', $dia);
        $query = $this->db->get($this->total_tables['tarifa']);
        if($query->num_rows() > 0){
            $row = $query->row_array();
            $this->db->where('id', $row['id']);
            $this->db->update($this->total_tables['tarifa'], array('importe' => $importe));
        }else{
            $data = array(
                'fecha' => $dia,
                'importe' => $importe,
                'fk_id_producto' => $producto,
                'fk_id_empresa' => $this->ion_auth->user()->row()->company
            );            
            $this->db->insert($this->total_tables['tarifa'], $data);           
        }
    }
    
    function eliminar_cierres($idcierre = '', $empresa = ''){
        $this->db->where('id', $idcierre);
        $this->db->where('fk_id_empresa', $empresa);
        $this->db->delete($this->total_tables['reserva']);
    }
    
    function confirmar_reservas($idcierre = '', $empresa = ''){
        $this->db->where('id', $idcierre);
        $this->db->where('fk_id_empresa', $empresa);
        $this->db->update($this->total_tables['reserva'], array('confirmada' => "S")); 
    }
}